package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;

/**
 * @projectName: qeug-cosmic-root-project
 * @package: qeug.shsh.opplugin
 * @className: ReimbursementBilPlugin
 * @author: xuhaihui
 * @description: 成本中心过滤
 * @date: 2024/3/6
 * @version: 1.0
 * 说明：成本中心过滤赋值逻辑：通过成本中心映射配置查询费用承担部门对应的成本中心
 * 使用单据：付款申请单，费用报销单，差旅报销单，借款单
 */

public class CostCenterFilter extends AbstractFormPlugin implements BeforeF7SelectListener {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit control = this.getView().getControl("std_costcenter");//成本中心
        control.addBeforeF7SelectListener(this);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        DynamicObject costDept = (DynamicObject) this.getModel().getValue("costdept");//费用承担部门
        DynamicObject costCompany = (DynamicObject) this.getModel().getValue("costcompany");//费用承担公司
        if (costDept == null || costCompany == null) {
            return;
        }
        QFilter qFilter1 = new QFilter("qeug_org.number", QCP.equals, costDept.getString("number"));
        DynamicObject bos_costcentersourcemap = BusinessDataServiceHelper.loadSingle("qeug_costcenter_org", new QFilter[]{qFilter1});//成本中心部门配置
        if (bos_costcentersourcemap == null) {
            return;
        }
        DynamicObjectCollection entryEntityCollection = bos_costcentersourcemap.getDynamicObjectCollection("qeug_entryentity");//分录
        List<String> costCenterNumbers = new ArrayList<>();
        for (DynamicObject entryEntity : entryEntityCollection) {
            DynamicObject costCenter1 = entryEntity.getDynamicObject("qeug_costcenter");//成本中心
            DynamicObject accountOrg = costCenter1.getDynamicObject("accountorg");//对应核算组织
            if (accountOrg != null && accountOrg.getString("number") != null && accountOrg.getString("number").equals(costCompany.getString("number"))) {
                String number = costCenter1.getString("number");
                if (number != null && !number.isEmpty()) {
                    costCenterNumbers.add(number);
                }
            }
        }
        if (costCenterNumbers.size() != 1) {
            this.getModel().setValue("std_costcenter", null);//成本中心
        } else {
            QFilter qFilter2 = new QFilter("number", QCP.equals, costCenterNumbers.get(0));
            DynamicObject bos_costcenter = BusinessDataServiceHelper.loadSingle("bos_costcenter", "id", new QFilter[]{qFilter2});//成本中心
            this.getModel().setValue("std_costcenter", bos_costcenter);//成本中心
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String fieldKey = e.getProperty().getName();
        if ("costdept".equals(fieldKey) || "costcompany".equals(fieldKey)) {
            //费用承担部门，费用承担公司
            DynamicObject costDept = (DynamicObject) this.getModel().getValue("costdept");//费用承担部门
            DynamicObject costCompany = (DynamicObject) this.getModel().getValue("costcompany");//费用承担公司
            if (costDept == null || costCompany == null) {
                this.getModel().setValue("std_costcenter", null);
                return;
            }
            QFilter qFilter1 = new QFilter("qeug_org.number", QCP.equals, costDept.getString("number"));
            DynamicObject bos_costcentersourcemap = BusinessDataServiceHelper.loadSingle("qeug_costcenter_org", new QFilter[]{qFilter1});//成本中心部门配置
            if (bos_costcentersourcemap == null) {
                this.getModel().setValue("std_costcenter", null);
                return;
            }
            DynamicObjectCollection entryEntityCollection = bos_costcentersourcemap.getDynamicObjectCollection("qeug_entryentity");//分录
            List<String> costCenterNumbers = new ArrayList<>();
            for (DynamicObject entryEntity : entryEntityCollection) {
                DynamicObject costCenter1 = entryEntity.getDynamicObject("qeug_costcenter");//成本中心
                DynamicObject accountOrg = costCenter1.getDynamicObject("accountorg");//对应核算组织
                if (accountOrg != null && accountOrg.getString("number") != null && accountOrg.getString("number").equals(costCompany.getString("number"))) {
                    String number = costCenter1.getString("number");
                    if (number != null && !number.isEmpty()) {
                        costCenterNumbers.add(number);
                    }
                }
            }
            if (costCenterNumbers.size() != 1) {
                this.getModel().setValue("std_costcenter", null);//成本中心
            } else {
                QFilter qFilter2 = new QFilter("number", QCP.equals, costCenterNumbers.get(0));
                DynamicObject bos_costcenter = BusinessDataServiceHelper.loadSingle("bos_costcenter", "id", new QFilter[]{qFilter2});//成本中心
                this.getModel().setValue("std_costcenter", bos_costcenter);//成本中心
            }
//            QFilter qFilter1 = new QFilter("entryentity.sourcedata.number", QCP.equals, costDept.getString("number"));
//            DynamicObject bos_costcentersourcemap = BusinessDataServiceHelper.loadSingle("bos_costcentersourcemap", "costcenter", new QFilter[]{qFilter1});//成本中心映射配置
//            if (bos_costcentersourcemap == null) {
//                this.getModel().setValue("std_costcenter", null);
//                return;
//            }
//            DynamicObject costCenter = bos_costcentersourcemap.getDynamicObject("costcenter");//成本中心
//            QFilter qFilter2 = new QFilter("number", QCP.equals, costCenter.getString("number"));
//            DynamicObject bos_costcenter = BusinessDataServiceHelper.loadSingle("bos_costcenter", "accountorg", new QFilter[]{qFilter2});
//            DynamicObject accountorg = bos_costcenter.getDynamicObject("accountorg");//对应核算组织
//            if (accountorg != null && accountorg.getString("number") != null && accountorg.getString("number").equals(costCompany.getString("number"))) {
//                //成本中心中的对应核算组织与费用承担公司一致才能给成本中心赋值
//                this.getModel().setValue("std_costcenter", costCenter);//成本中心
//            } else {
//                this.getModel().setValue("std_costcenter", null);//成本中心
//            }
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        ListShowParameter formShowParameter = (ListShowParameter) beforeF7SelectEvent.getFormShowParameter();
        List<QFilter> qFilter = new ArrayList<>();
        DynamicObject costDept = (DynamicObject) this.getModel().getValue("costdept");//费用承担部门
        DynamicObject costCompany = (DynamicObject) this.getModel().getValue("costcompany");//费用承担公司
        if (costDept == null || costCompany == null) {
            return;
        }
        QFilter qFilter1 = new QFilter("qeug_org.number", QCP.equals, costDept.getString("number"));
        DynamicObject bos_costcentersourcemap = BusinessDataServiceHelper.loadSingle("qeug_costcenter_org", new QFilter[]{qFilter1});//成本中心部门配置
        if (bos_costcentersourcemap == null) {
            qFilter.add(new QFilter("number", QCP.equals, "00000000000"));
            formShowParameter.getListFilterParameter().setQFilters(qFilter);
            return;
        }
        DynamicObjectCollection entryEntityCollection = bos_costcentersourcemap.getDynamicObjectCollection("qeug_entryentity");//分录
        List<String> costCenterNumbers = new ArrayList<>();
        for (DynamicObject entryEntity : entryEntityCollection) {
            DynamicObject costCenter1 = entryEntity.getDynamicObject("qeug_costcenter");//成本中心
            DynamicObject accountOrg = costCenter1.getDynamicObject("accountorg");//对应核算组织
            if (accountOrg != null && accountOrg.getString("number") != null && accountOrg.getString("number").equals(costCompany.getString("number"))) {
                String number = costCenter1.getString("number");
                if (number != null && !number.isEmpty()) {
                    costCenterNumbers.add(number);
                }
            }
        }
        if (!costCenterNumbers.isEmpty()) {
            qFilter.add(new QFilter("number", QCP.in, costCenterNumbers.toArray()));
            formShowParameter.getListFilterParameter().setQFilters(qFilter);
        } else {
            qFilter.add(new QFilter("number", QCP.equals, "00000000000"));
            formShowParameter.getListFilterParameter().setQFilters(qFilter);
        }
    }
}